home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume22 / pty / part02 < prev    next >
Encoding:
Internet Message Format  |  1990-10-09  |  54.0 KB

  1. Subject:  v23i032:  Run a program under a pty session, Part02/06
  2. Newsgroups: comp.sources.unix
  3. Approved: rsalz@uunet.UU.NET
  4. X-Checksum-Snefru: 6fe1a084 64d83c14 ac39c37e bf52d9f3
  5.  
  6. Submitted-by: Dan Bernstein <brnstnd@kramden.acf.nyu.edu>
  7. Posting-number: Volume 23, Issue 32
  8. Archive-name: pty/part02
  9.  
  10. #! /bin/sh
  11. # This is a shell archive.  Remove anything before this line, then feed it
  12. # into a shell via "sh file" or similar.  To overwrite existing files,
  13. # type "sh file -c".
  14. # The tool that generated this appeared in the comp.sources.unix newsgroup;
  15. # send mail to comp-sources-unix@uunet.uu.net if you want that tool.
  16. # Contents:  INSTALLREADABLE master.c pty.c
  17. # Wrapped by rsalz@litchi.bbn.com on Wed Oct 10 10:11:37 1990
  18. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  19. echo If this archive is complete, you will see the following message:
  20. echo '          "shar: End of archive 2 (of 6)."'
  21. if test -f 'INSTALLREADABLE' -a "${1}" != "-c" ; then 
  22.   echo shar: Will not clobber existing file \"'INSTALLREADABLE'\"
  23. else
  24.   echo shar: Extracting \"'INSTALLREADABLE'\" \(22621 characters\)
  25.   sed "s/^X//" >'INSTALLREADABLE' <<'END_OF_FILE'
  26. XThis is a typescript of INSTALL on a Sun 4 running SunOS 4.0.3.
  27. XI've inserted some further comments at spots where INSTALL might give
  28. Xdifferent results on different machines.
  29. X
  30. X
  31. X
  32. XHi, and welcome to the pty install script.
  33. Xpty is a program for managing pseudo-terminals.
  34. X
  35. XI'm not actually going to install anything.
  36. XI'll just guide you through what has to be done,
  37. Xfrom compiling through installation.
  38. X
  39. XOne advantage of this hands-off philosophy is that
  40. Xyou can kill and restart this script at any time.
  41. XYou may prefer to read through INSTALLREADABLE,
  42. Xwhich has the same information.
  43. X
  44. XIn the first part of this script, I'll lead you through configuration.
  45. XFor the moment, don't actually change anything in your system. Just
  46. Xalternate between reading this script and fooling around with the
  47. XMakefile, and I'll remind you later, after compilation, of what has
  48. Xto be changed elsewhere.
  49. X
  50. XI need the following programs: echo, tr [-d], man, sed [-n], grep, test.
  51. X
  52. X----- Press return to continue. 
  53. X
  54. XThe first versions of pty were designed on a BSD 4.2-based system.
  55. XDuring May 1990 pty 3.0 was written completely from scratch,
  56. Xon a BSD 4.3-based system. It has been thoroughly tested on
  57. Xseveral BSD 4.3-based systems, tested with a few Makefile changes
  58. Xon several BSD 4.2-based systems, and run on at least one mutant.
  59. X
  60. XIf you make it through installation and testing and get pty running,
  61. Xplease send a note to the author, Dan Bernstein, on the Internet
  62. Xat brnstnd@nyu.edu. Let him know your computer model, OS version, and
  63. Xwhat changes you had to make. If you have any trouble, please also get
  64. Xin touch with the author. If you have a different kind of system with
  65. Xpseudo-terminal support that could use a pty port, the author would
  66. Xlove to hear about it.
  67. X
  68. XOne note: Like all software, pty comes without warranty, to the extent
  69. Xpermitted by applicable law. Use it at your own risk.
  70. X
  71. X----- Press return to continue. 
  72. X
  73. XI assume that you don't want to make any major changes
  74. Xto your computer just to take advantage of pty's capabilities.
  75. XYou might not even have a privileged account, for all I know.
  76. X
  77. XThe first major decision you have to make is what privileges to give pty.
  78. XObviously if you don't have privileges then pty won't either;
  79. Xthis is Case UN.
  80. X
  81. X----- Press return to continue. 
  82. X
  83. XThere are several good reasons for giving programs privileges.
  84. XMost importantly, privileged programs can precisely control access
  85. Xto a shared resource. pty supports an interactive user list in
  86. X/etc/utmp, a login-logout record in /usr/adm/wtmp, and controlled
  87. Xaccess to the pseudo-terminal files themselves, including security and
  88. Xmode changing. None of these are possible if pty isn't setuid.
  89. X
  90. XIf your system has a lot more tty security than usual, you probably
  91. Xwon't be able to run pty without privileges. (However, if your system
  92. Xhas a lot more tty security than usual, it's almost certainly running
  93. Xpty anyway.)
  94. X
  95. X----- Press return to continue. 
  96. X
  97. XIf you do want pty to have privileges, you have to decide between
  98. Xsetting up pty as root (Case ROOT) or as some other user, say ``pty''
  99. X(Case PTY). Staying away from root has the advantage that problems
  100. Xcan't possibly destroy all security in one blow. However, root has
  101. Xseveral advantages, mainly because of BSD limitations: 1. A process
  102. Xcan't change ownership of a file between its euid and uid. Only root
  103. Xcan change ownership. It shouldn't be necessary to change ownership
  104. Xof ptys anyway, but that's what many programs assume. 2. MAXUPRC in
  105. X<sys/param.h> limits the number of processes under a given *effective*
  106. Xuid. This absolutely idiotic behavior is a serious problem for programs
  107. Xsetuid to anything other than root. pty tries to work around this
  108. Xproblem, but there are no true solutions. 3. Despite the documentation
  109. Xin kill(2), many machines don't allow non-root processes to send
  110. XCONT to children with a different uid. Unlike almost all other programs,
  111. Xpty sensibly handles its child stopping and restarting; but this won't
  112. Xwork on those machines if pty isn't root.
  113. X
  114. XYou should decide now between Case ROOT and Case PTY.
  115. X
  116. X----- Press return to continue. 
  117. X
  118. XWhether you've decided on ROOT, PTY, or UN,
  119. Xnow is the time to check the security of the entire pty package.
  120. XIt'd be difficult for anything to slip through the checks by the
  121. Xauthor, testers, and source group moderator, but you should take
  122. Xat least a moment to look for your pet security peeves.
  123. X
  124. XA couple of security notes: The only open(,O_CREAT) in pty is in
  125. Xmaster.c, at the top of master(), of a file with a very restricted
  126. Xform constructed from the filename of a /dev/ttyxx. There are no
  127. Xcreat()s. The only bind() is in sock.c's pty_readsock(), which is
  128. Xcalled only from master.c's master() to create a socket with a
  129. Xsimilarly restricted name. (fnsty is changed in sigler but not in
  130. Xmaster.) The only exec() is in slave.c, after a forced setreuid()
  131. Xto a uid that is only set to getuid(). unlink(), mkdir(), and rename()
  132. Xare only used with restricted pathnames.
  133. X
  134. X----- Press return to continue. 
  135. X
  136. XIn Case PTY, you have to set up a new uid, say ``pty'' (or whatever
  137. Xusername you want). pty should not allow logins. Its password should
  138. Xbe an asterisk; its home directory should be /nonexistent; its shell
  139. Xshould be /bin/true. In other words, the only access to user pty
  140. Xshould be through these setuid programs.
  141. X
  142. XIn any privileged case you should have group tty,
  143. Xwhich most BSD 4.3 systems have as gid 4. 
  144. X
  145. XRunning $ ttygroup="`sed -n 's/^tty:[^:]*:\(.*\):.*/\1/p' < /etc/group`" ...
  146. X
  147. XI see that you have a tty group, 4 as usual. Good.
  148. X
  149. X[Note: Under SunOS 4.0.3, ttygroup comes out to 4. On systems where it
  150. Xcomes out to nothing at all, you have to add the line tty:4:*:root to
  151. X/etc/group. On systems where the tty group is something other than 4,
  152. Xjust remember it.]
  153. X
  154. X----- Press return to continue. 
  155. X
  156. XNext, we're going to drudge through the config.h file.
  157. X
  158. XIn Cases ROOT or PTY, you have to set up a system-wide directory
  159. Xfor storing information about pty sessions. (A session is a program
  160. Xstuck under a pty that you can disconnect and reconnect.) Users can
  161. Xget around this directory, storing the information in ~/.pty and
  162. Xrevoking any privileges, with pty -xS; you can even make this default
  163. Xby setting flagxsetuid = 0 in globals.c. However, a single system-wide
  164. Xdirectory is safer.
  165. X
  166. XAnyway, that directory, PTYDIR, should be mode 0700, owner root in case
  167. XROOT or pty in case PTY, group irrelevant. PTYDIR is defined in config.h
  168. Xas /usr/etc/pty by default; if you choose a different directory, add
  169. X-DPTYDIR=\"/what/ever/dir/ect/ory\" to CCOPTS in the Makefile.
  170. X
  171. XIn Case UN, you may want to set up a directory ~/PTY inside your home
  172. Xdirectory, and set PTYDIR to that; you may want to set flagxsetuid = 0
  173. Xin globals.c; or you may want to just type -xS to pty all the time. I
  174. Xrecommend the first strategy.
  175. X
  176. X----- Press return to continue. 
  177. X
  178. XNext come pseudo-terminal pathnames. I assume that your ptys are
  179. Xlabelled as /dev/ptyxx and /dev/ttyxx, where xx is any two-letter
  180. Xextension. pty can support any initial strings instead of these,
  181. Xbut some of the utilities have /dev/tty hardcoded, and lots of other
  182. Xprograms running around assume those names. If you're desperate,
  183. Xdefine DEVMTY and DEVSTY in the Makefile, and figure out what has to
  184. Xbe changed in util/*. (Sorry.)
  185. X
  186. X----- Press return to continue. 
  187. X
  188. XThe extension is traditionally [p-za-o][0-9a-f]. The first letter of the
  189. Xsecond string is special: if /dev/ttyq0 doesn't exist, for example, then
  190. Xnone of /dev/ttyq[0-9a-f] will be used. (That's called a pty bank.)
  191. X
  192. XYou can change those strings by defining, e.g., PTY1=\"ABCDEFG\" or
  193. XPTY2=\"02468abfq\" in the Makefile. You have at least four choices here:
  194. X1. Give pty its own, new, banks of new pseudo-terminals with weird bank
  195. Xnames not including a through z, and define PTY1 appropriately. pty will
  196. Xonly use those new ptys, and older programs will just use the old ptys.
  197. XThis may require reconfiguring your system for the larger number of
  198. Xterminals. 2. Give pty some new banks, and take out some old ones to
  199. Xmake up for it. 3. Give pty a chunk of the old banks. 4. Give pty all
  200. Xthe old banks.
  201. X
  202. XNote that pty searches randomly through PTY1 and PTY2 by default; for
  203. Xefficiency, PTY1 really should reflect exactly the pty banks you have.
  204. X
  205. X----- Press return to continue. 
  206. X
  207. XSo now you've decided on new ptys, or set aside some or all of your
  208. Xold ptys, and changed PTY1 and PTY2 appropriately. Because BSD gives a
  209. Xprocess very little control over its controlling terminal, pty has to
  210. Xpass a terminal name explicitly when it reconnects. If any of your tty
  211. X(not just pseudo-tty!) filenames are longer than 30 characters, you have
  212. Xto set -DTTYNAMELEN=60 (or whatever) in the Makefile.
  213. X
  214. X----- Press return to continue. 
  215. X
  216. X/etc/utmp has traditionally listed all users logged on.
  217. XThis nebulous concept has evolved through the years; given the way
  218. Xthat most programs use utmp, utmp is probably better defined as a
  219. Xlist of all interactive sessions. Anyway, pty supports this file,
  220. Xand will make an entry in it when given -xu. If you have a different
  221. Xfile, add -DPTYUTMP_FILE=\"/foo/bar/utmp\" to DEFINES in the Makefile.
  222. X
  223. XNote that /etc/utmp is unprotected (mode 666) on Suns. This was Sun's
  224. Xattempt to let unprivileged programs manage the file. Unforunately, this
  225. Xerror in judgment opens up a huge security hole, which even on
  226. Xsingle-user machines is an invitation to make mistakes. I advise you to
  227. Xmake /etc/utmp owned by root in Case ROOT or pty in Case PTY, mode 644.
  228. XThere aren't many unprivileged programs that don't understand the
  229. Xpty interface; people on non-Suns have survived so far, and there's
  230. Xno reason for you to keep subscribing to an insecure model of resource
  231. Xsharing.
  232. X
  233. X----- Press return to continue. 
  234. X
  235. XAlthough pty will put an entry into utmp, it doesn't really support
  236. Xthe remote-host field. There is no logical association between sessions
  237. Xand connections; why should a login require a pty, and why should a
  238. Xsession only stick around for one connection? So pty just puts "pty" in
  239. Xthe host field. You can change this with -DPTYUTMP_HOST=\"foo\"; you may
  240. Xeven want to make PTY_UTMPHOST call a function that you define.
  241. X
  242. X(Similar comments apply to PTYWTMP_HOST. There are also PTYUTMP_SWHOST
  243. Xand PTYWTMP_SWHOST, defaults PTYUTMP_HOST and "pty-sessuser"
  244. Xrespectively; see util/sessuser.1 for more information.)
  245. X
  246. X----- Press return to continue. 
  247. X
  248. X/usr/adm/wtmp has traditionally recorded all logins, logouts, reboots,
  249. Xftp sessions, and various other interesting events. (The entry for a
  250. Xline in utmp is the last entry for that line in wtmp.) pty supports wtmp
  251. Xfully, in the same way as utmp. You should make wtmp mode 644, owner
  252. Xroot or pty as appropriate. Change PTYWTMP_FILE if /usr/adm/wtmp is
  253. Xsomewhere else.
  254. X
  255. X----- Press return to continue. 
  256. X
  257. XYou can turn off utmp support by uncommenting the NO_UTMP line in
  258. Xconfig.h. (Just delete the initial /*.) You can also turn off support
  259. Xfor wtmp, disconnectable sessions, or changing pseudo-terminal file
  260. Xownership; just uncomment the appropriate line. You can force use of
  261. Xeach of these features by defining MUST_UTMP, etc., in the same way.
  262. X
  263. XNote that the usual login programs do their own wtmp management, and
  264. Xmany accounting programs incorrectly equate session time with connect
  265. Xtime, so users will rarely want wtmp. As more programs appear using the
  266. Xpty interface, and as login programs start using a more logical system
  267. Xfor accounting, MUST_WTMP may become an appropriate way to monitor
  268. Xpseudo-terminal usage. For the moment, I do not advise setting any NO
  269. Xor MUST.
  270. X
  271. X----- Press return to continue. 
  272. X
  273. Xpty subscribes (not very willingly) to the BSD 4.3 model of pty
  274. Xprotection, with a few twists. All ptys are under group tty, at all
  275. Xtimes; if you don't have a tty group, all ptys should be under some
  276. Xother protected group at all times. Anyway, pty will change the group of
  277. Xeach pseudo-terminal file to PTYGROUP, default 4, under -xc.
  278. X
  279. X
  280. XSince you already have a tty group, gid 4, you're fine.
  281. X
  282. X[Note: If your tty group is something other than 4, add -DPTYGROUP=6 (or
  283. Xwhatever) to the Makefile.]
  284. X
  285. X
  286. X(Under Case UN, don't worry about all this group stuff. Just leave
  287. XPTYGROUP alone.)
  288. X
  289. X----- Press return to continue. 
  290. X
  291. XUnder -xc, pty changes the pseudo-terminal owner to the current real
  292. Xuid. The fact that terminals need to be in the file hierarchy reflects a
  293. Xserious failure in the BSD terminal model; but anyway, many programs
  294. Xexpect to be able to fool around with /dev/tty. After pty is done with
  295. Xthe terminal, it sets the owner back to its effective uid. You can
  296. Xmodify this behavior by changing PTYOWNER from euid to something else.
  297. X
  298. XUnder Case UN, PTYOWNER is irrelevant.
  299. X
  300. X----- Press return to continue. 
  301. X
  302. XWhile a tty is unused, BSD systems traditionally leave it unprotected.
  303. XUnfortunately, this leaves some huge security holes. Here's one of
  304. Xthe biggest advantages of a privileged pty manager: unused ttys don't
  305. Xhave to be left open for any random program to use. After it's done
  306. Xwith a pseudo-terminal, pty changes it to mode UNUSEDPTYMODE, default
  307. X0600. (This is another case of a poor model for kludging pty support
  308. Xinto unprivileged programs. If you haven't set up separate pty banks
  309. Xfor pty, and you really want to allow unprivileged access to unused
  310. Xptys, try mode 0660 and make those other programs setgid to tty.)
  311. X
  312. XA tty that someone's using can be in many different modes. Typically
  313. XUSEDPTYMODE should be 0600, which means user-only access, messages off,
  314. Xbiff off. If you really, really, really want to make the mistake of
  315. Xhaving messages or biff on by default, try 0620, 0700, or 0720. Note
  316. Xthat the world protection should always be 0: the point of the tty group
  317. Xis that only setgid-tty programs can access ttys.
  318. X
  319. XAs usual, you can't do anything about these in Case UN.
  320. X
  321. X----- Press return to continue. 
  322. X
  323. XNow we're past generic configuration and down to your system's
  324. Xnitty-gritty.
  325. X
  326. XFor purity, pty has SIGRET_TYPE, default int, as the type returned by a
  327. Xsignal handler. On Suns and ANSI-compliant machines, you should define
  328. XSIGRET_TYPE=void. Then you can admire the lint -haxc *.c output.
  329. X
  330. X----- Press return to continue. 
  331. X
  332. XOUTBUFSIZE, default 16384, is the size of the buffer pty keeps between
  333. Xthe incoming data and the pseudo-tty, and between the pseudo-tty and the
  334. Xoutgoing data. If you want to spare less than 32K per pty just for
  335. Xbuffer space, feel free to change this.
  336. X
  337. X----- Press return to continue. 
  338. X
  339. XGENERIC is another concession to ANSI taste. GENERIC * should be a
  340. Xpointer type that any other pointer can be safely converted to and back;
  341. XGENERIC is char by default, but on newer machines can be set to void.
  342. XAs it'll be at least five years before ANSI manages to outlaw char *,
  343. Xand as void * is often invalid, I advise you to leave GENERIC alone.
  344. X
  345. X----- Press return to continue. 
  346. X
  347. Xpty attempts to forward window-size changes transparently. It also
  348. Xsupports the auxiliary characters structure, which lets you type a
  349. Xsingle character to get system and tty status.
  350. X
  351. XRunning $ man 4 tty | sed 's///g' | grep -s winsize
  352. XReformatting page.  Wait... done
  353. X
  354. XI see you don't have window sizes.
  355. XYou'll have to comment out #define TTY_WINSIZE in config.h.
  356. X(This may be inaccurate on Suns; try grep winsize /usr/include/sys/tt*.h.)
  357. X
  358. X[Note: As a matter of fact, the test is inaccurate under SunOS 4.0.3,
  359. Xwhich does have window sizes. If you have window sizes, you don't have
  360. Xto do anything.]
  361. X
  362. XRunning $ man 4 tty | sed 's///g' | grep -s auxchars
  363. XReformatting page.  Wait... done
  364. X
  365. XI see you don't have auxiliary characters.
  366. X
  367. X[Note: This is the usual. If you have auxiliary characters and want pty
  368. Xto understand them, uncomment #define TTY_AUXCHARS in config.h.]
  369. X
  370. X----- Press return to continue. 
  371. X
  372. Xpty also puts siginterrupt() and usleep() to good use in working
  373. Xaround BSD limitations. Without siginterrupt(), there is absolutely
  374. Xno way to get per-process non-blocking I/O, so pty may block when it
  375. Xdoesn't have to (namely, when it has N bytes to write to the output,
  376. Xthe output is a pipe with M bytes of space, and N > M > 0). If you
  377. Xdefine SIGINTERRUPT (as default), pty will take some extra effort to
  378. Xnever, ever, ever block when it doesn't have to.
  379. X
  380. XAt one point, pty pauses to kludge around a common bug in UNIX-domain
  381. Xsockets. With usleep() it will pause much more briefly. This makes
  382. Xreconnects much faster.
  383. X
  384. XRunning $ man siginterrupt | sed 's///g' | grep -s siginterrupt
  385. XReformatting page.  Wait... done
  386. X
  387. XI see you have siginterrupt(). Good.
  388. X
  389. XWeird, are you BSD 4.3?
  390. X
  391. X[Note: Without siginterrupt(), you have to comment out #define
  392. XSIGINTERRUPT in config.h.]
  393. X
  394. XRunning $ man usleep | sed 's///g' | grep -s usleep
  395. XReformatting page.  Wait... done
  396. X
  397. XI see you have usleep(). Good.
  398. X
  399. X[Note: Without usleep(), you have to comment out #define USLEEP in
  400. Xconfig.h.]
  401. X
  402. X----- Press return to continue. 
  403. X
  404. XFinally (almost done with configuration!), we have to make sure that you
  405. Xhave UNIX-domain sockets, and check whether you have file descriptor
  406. Xpassing.
  407. X
  408. XRunning $ test -r /usr/include/sys/un.h
  409. X
  410. XI see you have UNIX-domain sockets. Good.
  411. X
  412. X[Note: If <sys/un.h> isn't there, you probably don't have UNIX-domain
  413. Xsockets, which means you can't have disconnectable sessions. Define
  414. XNO_UNIXSOCKS in config.h.]
  415. X
  416. XRunning $ man recvmsg | sed 's///g' | grep -s rights
  417. XReformatting page.  Wait... done
  418. X
  419. XI see you have file descriptor passing. Good. (Your fd passing may be
  420. Xbuggy, as it's a relatively new, powerful, and rarely exploited
  421. Xfeature. If you have trouble, try defining NO_FDPASSING.)
  422. X
  423. X[Note: If there isn't a recvmsg() man page mentioning access rights, you
  424. Xprobably don't have file descriptor passing. This isn't a disaster,
  425. Xthough it means that reconnected sessions will be as slow as they are in
  426. Xprograms other than pty. Define NO_FDPASSING.]
  427. X
  428. X----- Press return to continue. 
  429. X
  430. XWhew! We've finally made it through configuration. If you want, try
  431. Xrunning lint -haxc *.c or whatever your favorite code checker is; the
  432. Xlint here gives some bogus errors about correct char * casts, an error
  433. Xbecause the SIG_IGN definition is unportable, and a couple of bzero
  434. Xcomplaints because FD_ZERO isn't defined very well.
  435. X
  436. XIf you're on an old system without FD_ZERO, FD_SET, and FD_ISSET in
  437. X<sys/types.h>, I recommend you upgrade. Try copying file.h.old to file.h
  438. X(the original version is in file.h.new).
  439. X
  440. XWe're nearly at the end. Change CC and CCOPTS in the Makefile for any
  441. Xlast-minute additions; if you're worried, change -s to -g for debugging.
  442. XNow compile! % (date; make) >>& Makelog & and come back to this script.
  443. X
  444. X----- Press return to continue. 
  445. X
  446. XWhile the program is compiling, let's start providing some support.
  447. X(If the compile finishes or has an error, just ignore it for the
  448. Xmoment.) These are some real changes, so watch out!
  449. X
  450. XFirst---in Case PTY only!---make a new uid, pty, not allowing logins.
  451. X
  452. X/etc/passwd:  pty:*:whatever:4:::/bin/true
  453. X
  454. X----- Press return to continue. 
  455. X
  456. XSecond, make sure you have a tty group set up.
  457. X
  458. X/etc/group:  tty:*:4:root
  459. X
  460. X----- Press return to continue. 
  461. X
  462. XThird, make PTYDIR.
  463. X
  464. XROOT: # mkdir /usr/etc/pty; chmod 700 /usr/etc/pty
  465. XPTY: # mkdir /usr/etc/pty; chown pty /usr/etc/pty; chmod 700 /usr/etc/pty
  466. XUN: % mkdir ~/PTY; chmod 700 ~/PTY
  467. X
  468. X----- Press return to continue. 
  469. X
  470. XFourth, make sure you have all the pseudo-terminal files set up and
  471. Xappropriately configured.
  472. X
  473. XChange each of them to group tty.
  474. X
  475. XChange each of them to owner pty in case PTY or root in case ROOT.
  476. X
  477. XChange each of them to mode UNUSEDPTYMODE (0600).
  478. X
  479. X----- Press return to continue. 
  480. X
  481. XFifth, make sure /etc/utmp and /usr/adm/wtmp exist, owned by root in
  482. XCase ROOT or pty in Case PTY, each mode 644. (In Case UN, you probably
  483. Xwant to be working on a Sun with its insecure /etc/utmp, so that pty can
  484. Xtake advantage of it. Sigh.)
  485. X
  486. X----- Press return to continue. 
  487. X
  488. XSixth, I advise that you make a special directory /usr/local/ptybin for
  489. Xpty and its associated utilities, with symbolic links in /usr/local.
  490. XThis choice isn't too important, but it's easier if you know a
  491. Xplace to put programs.
  492. X
  493. X----- Press return to continue. 
  494. X
  495. XOkay. You've done about all that can be done before compilation
  496. Xfinishes, so go do something else.
  497. X
  498. X----- Press return to continue. 
  499. X
  500. XThe compile should be done now. If all went well, the make should have
  501. Xexited normally, Makelog should show no errors, and you should have an
  502. Xexecutable pty program sitting in this directory. Unfortunately, life
  503. Xisn't always so generous. Here are a few of the more common errors.
  504. X
  505. XThe loader complains about missing getopt(), optarg, and optind: These
  506. Xare all in the getopt library, available in the early volumes of
  507. Xcomp.sources.unix. If you use GNU getopt, please make sure you
  508. Xunderstand the implications of its license.
  509. X
  510. XThe loader complains about missing FD_ZERO, FD_SET, and FD_ISSET: Try
  511. Xcopying file.h.old to file.h and recompiling. If the compiler gives
  512. Xvarious further errors related to these macros, uncomment the
  513. Xcommented-out lines in file.h.old, copy to file.h, and try once again.
  514. X
  515. Xbcopy() isn't defined: In pty.h, change bcopy(src,dst,num) to
  516. Xmemcpy(dst,src,num). (Note the order.) Try again.
  517. X
  518. XIf you still can't get the code to compile, let the author know.
  519. X
  520. X----- Press return to continue. 
  521. X
  522. XNext, compile the utilities. There isn't much to say about them;
  523. Xthey're all public domain, some of them clones of or improvements
  524. Xover standard utilities, some of them clones with special features to
  525. Xwork with pty, some of them entirely new programs for pty's new
  526. Xfeatures. Just glance at the top of util/Makefile; then
  527. X% cd util; (date; make) >>& Makelog & and wait.
  528. X
  529. XWarning: The pty session directory is hardcoded in util/sessutil.c.
  530. XIf you have changed PTYDIR, check through sessutil.c carefully.
  531. X
  532. X----- Press return to continue. 
  533. X
  534. XSo now you should have some executables sitting around collecting dust.
  535. XIn Cases ROOT and PTY, put {pty,biff,disconnect,mesg,reconnect,sesskill,
  536. Xsesslist,sessname,sessuser} into a system-wide directory, owner root or
  537. Xpty, mode 4755. Put wall and write into the same place, group tty, mode
  538. X2755. Put {condom,excloff,exclon,last,lock,script,script.tidy,sess,tiocsti,
  539. Xtty,u,who,xdisconnect,xreconnect,xsesskill,xsesslist,xsessname,xsessuser}
  540. Xinto the same place, mode 755. Make absolutely sure none of the shell
  541. Xscripts are setuid.
  542. X
  543. XIn Case UN, just put all the programs somewhere in your PATH.
  544. X
  545. XThat's it! Try running TESTS now.
  546. X
  547. X(Two other things you have to do at some point: move your old script,
  548. Xbiff, mesg, and so on to script.old, biff.old, mesg.old, etc., with
  549. Xsymbolic links to the new versions in their place; and move the old
  550. X/usr/man/{script,biff,mesg,...}.1 somewhere else, copy all the *.1
  551. Xhere to /usr/man/man1, and run /etc/catman.)
  552. END_OF_FILE
  553.   if test 22621 -ne `wc -c <'INSTALLREADABLE'`; then
  554.     echo shar: \"'INSTALLREADABLE'\" unpacked with wrong size!
  555.   fi
  556.   # end of 'INSTALLREADABLE'
  557. fi
  558. if test -f 'master.c' -a "${1}" != "-c" ; then 
  559.   echo shar: Will not clobber existing file \"'master.c'\"
  560. else
  561.   echo shar: Extracting \"'master.c'\" \(18086 characters\)
  562.   sed "s/^X//" >'master.c' <<'END_OF_FILE'
  563. X/* Copyright 1990, Daniel J. Bernstein. All rights reserved. */
  564. X
  565. X#include <sys/types.h>
  566. X#include <sys/time.h>
  567. X#include <sys/resource.h>
  568. X#include <sys/wait.h>
  569. X#include <stdio.h>
  570. X#include "err.h"
  571. X#include "config.h"
  572. X#include "pty.h"
  573. X#include "master.h"
  574. X#include "sig.h"
  575. X#include "tty.h"
  576. X#include "file.h"
  577. X#include "sock.h"
  578. X#include "logs.h"
  579. X#include "misc.h"
  580. X
  581. Xstatic char fnre[20];
  582. X
  583. Xstatic char fnsess[20];
  584. Xstatic int fdsess;
  585. X
  586. Xstatic char *glfnsty;
  587. X
  588. Xstatic char soutbuf[OUTBUFSIZE];
  589. Xstatic char sptybuf[OUTBUFSIZE];
  590. X
  591. Xstatic struct ttymodes tmowinpty;
  592. Xstatic struct ttymodes tmowintty;
  593. X
  594. Xstatic char *outbuf = soutbuf;
  595. Xstatic int outbufsiz = OUTBUFSIZE;
  596. Xstatic int outbuflen = 0;
  597. Xstatic char *ptybuf = sptybuf;
  598. Xstatic int ptybufsiz = OUTBUFSIZE;
  599. Xstatic int ptybuflen = 0;
  600. X
  601. Xstatic int flagconnected = 1; /* 0: disconnected. 2: idling for stop. */
  602. X                              /* 3: idling for stop but child is dead. */
  603. Xstatic int flagchild = 1; /* 0: dead. 2: stopped. */
  604. Xstatic int childsig; /* signal that stopped/killed child */
  605. Xstatic int flagsigler = 1; /* 0: dead. */
  606. Xstatic int siglerpid; /* only defined if flagconnected */
  607. Xstatic int slavepid;
  608. X
  609. Xstatic int flagqwinch = 0;
  610. X
  611. Xstatic void quickdeath(i)
  612. Xint i;
  613. X{
  614. X /* All exits from master() go through here. */
  615. X if (flagsession) (void) unlink(fnsess);
  616. X if (flagxchown)
  617. X   (void) fchown(fdsty,PTYOWNER,PTYGROUP);
  618. X (void) fchmod(fdsty,UNUSEDPTYMODE);
  619. X date = now();
  620. X if (flagxutmp)
  621. X   if (utmp(glfnsty + PTYUTMP_OFFSET,"","",date) == -1)
  622. X     ; /* too bad. */
  623. X if (flagxwtmp)
  624. X   if (wtmp(glfnsty + PTYWTMP_OFFSET,"","",date) == -1)
  625. X     ; /* too bad. */
  626. X fatal(i);
  627. X}
  628. X
  629. Xstatic void death(i)
  630. Xint i;
  631. X{
  632. X (void) kill(siglerpid,SIGTERM);
  633. X /* XXX: should wait while flagsigler */
  634. X quickdeath(i);
  635. X}
  636. X
  637. X/*ARGSUSED*/
  638. Xstatic void sig_force(i)
  639. Xsig_num i;
  640. X{
  641. X /* Forced death, presumably from the sesskill program. */
  642. X sig_ignore(SIGCHLD);
  643. X /* XXX: Should we test for !flagchild here? sesskill does. */
  644. X flagchild = 0;
  645. X quickdeath(SIGCHLD);
  646. X}
  647. X
  648. X/*ARGSUSED*/
  649. Xstatic void sig_usr2(i)
  650. Xsig_num i;
  651. X{
  652. X if (flagsession)
  653. X  {
  654. X   int newuid = uid;
  655. X   char newsuid[10];
  656. X   char foo[100];
  657. X
  658. X   /* XXX: We should have some error recovery here! */
  659. X
  660. X   (void) lseek(fdsess,(long) 0,0);
  661. X   (void) read(fdsess,(char *) &newuid,sizeof(int));
  662. X   (void) sprintf(newsuid,"%d",newuid);
  663. X
  664. X   (void) chdir("..");
  665. X   if (chdir(newsuid) == -1)
  666. X    {
  667. X     (void) mkdir(newsuid,0700);
  668. X     (void) chdir(newsuid);
  669. X    }
  670. X
  671. X   (void) sprintf(foo,"../%d/%s",uid,fnsess);
  672. X   (void) rename(foo,fnsess);
  673. X
  674. X   (void) sprintf(foo,"../%d/%s",uid,fnre);
  675. X   (void) rename(foo,fnre); /* in case we're already disconnected */
  676. X
  677. X   uid = newuid;
  678. X   (void) setreuid(uid,euid);
  679. X   setusername();
  680. X
  681. X   if (flagxutmp)
  682. X     if (utmp(glfnsty + PTYUTMP_OFFSET,username,PTYUTMP_SWHOST,date) == -1)
  683. X       ; /* too bad. */
  684. X   if (flagxwtmp)
  685. X     if (wtmp(glfnsty + PTYWTMP_OFFSET,username,PTYWTMP_SWHOST,date) == -1)
  686. X       ; /* too bad. */
  687. X   if (flagsigler)
  688. X     (void) kill(siglerpid,SIGUSR2);
  689. X  }
  690. X}
  691. X
  692. X/*ARGSUSED*/
  693. Xstatic void sig_pipe(i)
  694. Xsig_num i;
  695. X{
  696. X flagsigler = 0; /* XXX: is this appropriate? race? */
  697. X /* Will end up giving child HUP. */
  698. X}
  699. X
  700. X/*ARGSUSED*/
  701. Xstatic void sig_chld(i)
  702. Xsig_num i;
  703. X{
  704. X union wait w;
  705. X
  706. X if (wait3(&w,WNOHANG | WUNTRACED,(struct rusage *) 0) <= 0)
  707. X   return; /* why'd we get the CHLD? it must have stopped & restarted? */
  708. X
  709. X if (w.w_stopval == WSTOPPED)
  710. X  {
  711. X   childsig = w.w_stopsig;
  712. X   flagchild = 2;
  713. X  }
  714. X else
  715. X  {
  716. X   childsig = w.w_termsig; /* can't do much with this */
  717. X   flagchild = 0;
  718. X  }
  719. X}
  720. X
  721. X/*ARGSUSED*/
  722. Xstatic void sig_term(i)
  723. Xsig_num i;
  724. X{
  725. X flagsigler = 0;
  726. X}
  727. X
  728. X/* If we have made it to being master, we should never get TTIN or TTOU, */
  729. X/* except possibly while restarting after a stop (e.g., if the user puts */
  730. X/* us back into the background). But we let the signaller handle putting */
  731. X/* the tty modes back before restarting us, so we should never, ever, */
  732. X/* ever get a TTIN or TTOU. If the user is messing around and we do get */
  733. X/* a TTIN or TTOU, we'll just pretend the child died and hope we get */
  734. X/* around to telling the signaller about it. */
  735. X
  736. X/*ARGSUSED*/
  737. Xstatic void sig_ttin(i)
  738. Xsig_num i;
  739. X{
  740. X if (flagchild)
  741. X  {
  742. X   childsig = SIGTTIN;
  743. X   flagchild = 2;
  744. X  }
  745. X}
  746. X
  747. X/*ARGSUSED*/
  748. Xstatic void sig_ttou(i)
  749. Xsig_num i;
  750. X{
  751. X if (flagchild)
  752. X  {
  753. X   childsig = SIGTTOU;
  754. X   flagchild = 2;
  755. X  }
  756. X}
  757. X
  758. X/*ARGSUSED*/
  759. Xstatic void sig_tstp(i)
  760. Xsig_num i;
  761. X{
  762. X if (flagchild)
  763. X  {
  764. X   childsig = SIGCONT;
  765. X   flagchild = 2;
  766. X  }
  767. X}
  768. X
  769. X/* Most job-control shells (including csh) behave absolutely miserably. */
  770. X/* (Well, that goes without saying.) In particular, rather than sending */
  771. X/* a CONT to every one of their children in the process group, they feel */
  772. X/* a need to kill the entire process group. Grrrr. Because of this, we */
  773. X/* are forced to use the nonintuitive USR1 to communicate CONT, and ignore */
  774. X/* CONT entirely. Anyway, read cont as usr1 where necessary. */
  775. X
  776. X/* We can only get USR1 from the signaller (or from us after reconnect). */
  777. X/* By convention, the signaller handles setting the tty modes back to */
  778. X/* chartty, even though we handled restoring the modes before stop. */
  779. X
  780. X/*ARGSUSED*/
  781. Xstatic void sig_cont(i)
  782. Xsig_num i;
  783. X{
  784. X if (flagchild)
  785. X  {
  786. X   flagchild = 1;
  787. X   (void) kill(slavepid,SIGCONT);
  788. X   (void) kill(pid,SIGWINCH);
  789. X  }
  790. X if (flagconnected == 3)
  791. X   flagconnected = 1; /* XXX: should be internal to master() */
  792. X (void) setpgrp(0,pgrp);
  793. X}
  794. X
  795. X/* If it weren't for WINCH, which must be in the master if NO_FDPASSING, */
  796. X/* and for the stupid conventions surrounding a process's control tty, */
  797. X/* then all mention of fdtty could disappear from master. This would */
  798. X/* slightly complicate the signaller's T{STP,TIN,TOU} handling but make */
  799. X/* reconnect a lot simpler. Sigh. */
  800. X
  801. X/*ARGSUSED*/
  802. Xstatic void sig_winch(i)
  803. Xsig_num i;
  804. X{
  805. X int pg;
  806. X
  807. X flagqwinch = 0;
  808. X#ifdef TTY_WINDOWS
  809. X/* An unfortunate but slight race: Another handler could change the pgrp */
  810. X/* if the child suddenly stops and we're queued for delivery. So we have */
  811. X/* to change it back. */
  812. X pg = getpgrp(0);
  813. X (void) setpgrp(0,pgrp);
  814. X if (!flagsigler)
  815. X   flagqwinch = 1;
  816. X else
  817. X   if (tty_getmodes(fdsty,&tmopty) == 0)
  818. X     if (tty_getmodes(fdtty,&tmowintty) == 0)
  819. X      {
  820. X       tty_copymodes(&tmowinpty,&tmopty);
  821. X       tty_copywin(&tmowinpty,&tmowintty);
  822. X       (void) tty_modifymodes(fdsty,&tmowinpty,&tmopty);
  823. X      }
  824. X (void) setpgrp(0,pg);
  825. X#endif
  826. X}
  827. X
  828. Xstatic int disconnect(fnsty)
  829. Xchar *fnsty;
  830. X{
  831. X if (fdtty != -1)
  832. X  {
  833. X   (void) tty_dissoc(fdtty); /* must succeed */
  834. X   (void) close(fdtty);
  835. X   fdtty = -1;
  836. X  }
  837. X if (fdpass != -1)
  838. X  {
  839. X   /* We used to write the dot to fdpass here. It's in sigler now, to */
  840. X   /* prevent a race condition. */
  841. X   (void) close(fdpass);
  842. X   fdpass = -1;
  843. X  }
  844. X if (fdin != -1)
  845. X  {
  846. X   (void) close(fdin);
  847. X   fdin = -1;
  848. X  }
  849. X if (fdout != -1)
  850. X  {
  851. X   (void) close(fdout);
  852. X   fdout = -1;
  853. X  }
  854. X if (fdre != -1)
  855. X  {
  856. X   (void) close(fdre);
  857. X   fdre = -1;
  858. X  }
  859. X
  860. X fdre = pty_readsock(fnsty,fnre);
  861. X if (fdre == -1)
  862. X   return -1; /* damn. */
  863. X return 0;
  864. X}
  865. X
  866. Xstatic int reconnect()
  867. X{
  868. X int t;
  869. X char buf[1];
  870. X char fntty[TTYNAMELEN]; /* sigh */
  871. X int flags = 0;
  872. X
  873. X t = pty_acceptsock(fdre);
  874. X (void) close(fdre);
  875. X fdre = t;
  876. X if (fdre == -1)
  877. X   return -1;
  878. X
  879. X#define VCF (void) close(fdre)
  880. X#define BONK(xxx,yyy) if ((xxx) == -1) { VCF; return -1; } else (yyy);
  881. X
  882. X/* What about fd 2 for warnings & errors? No, master doesn't use them. */
  883. X
  884. X/* Must have: in, out, siglerpid, pgrp, flagjobctrl. 1, 2, 16, 32, 256. */
  885. X/* Except if NO_FDPASSING: just flagjobctrl in that case. */
  886. X/* If fdtty, must have also tmochartty, tmotty, fntty. 8: 64, 128, 1024. */
  887. X/* Finally, fdpass is independent of all the rest. */
  888. X
  889. X/* CHANGE: With fdpass, fdin and fdout are irrelevant. */
  890. X
  891. X if (pty_sendint(fdre,'p',&pid) == -1)
  892. X  {
  893. X   VCF;
  894. X   return -1;
  895. X  }
  896. X
  897. X while (pty_getch(fdre,buf) == 0)
  898. X   switch(buf[0])
  899. X    {
  900. X#ifdef NO_FDPASSING
  901. X     case 's': BONK(pty_putgetstr(fdre,'s',fntty),flags |= 8) break;
  902. X#else
  903. X     case '0': BONK(pty_putgetfd(fdre,'0',&fdin),flags |= 1) break;
  904. X     case '1': BONK(pty_putgetfd(fdre,'1',&fdout),flags |= 2) break;
  905. X     case 'f': BONK(pty_putgetfd(fdre,'f',&fdpass),flags |= 4) break;
  906. X     case 't': BONK(pty_putgetfd(fdre,'t',&fdtty),flags |= 8) break;
  907. X     case 's': BONK(pty_putgetstr(fdre,'s',fntty),flags |= 1024) break;
  908. X#endif
  909. X     case 'p': BONK(pty_putgetint(fdre,'p',&siglerpid),flags |= 16) break;
  910. X     case 'g': BONK(pty_putgetint(fdre,'g',&pgrp),flags |= 32) break;
  911. X     case 'c': BONK(pty_putgettty(fdre,'c',&tmochartty),flags |= 64) break;
  912. X     case 'n': BONK(pty_putgettty(fdre,'n',&tmotty),flags |= 128) break;
  913. X     case 'j': BONK(pty_putgetint(fdre,'j',&flagjobctrl),flags |= 256) break;
  914. X#ifdef NO_FDPASSING
  915. X     case ' ': if ((flags & 256) != 256) { VCF; return -1; }
  916. X#else
  917. X     case ' ': if (flags & 4) flags |= 3;
  918. X           if ((flags & 307) != 307) { VCF; return -1; }
  919. X           if (flags & 8) if ((flags & 1024) != 1024) { VCF; return -1; }
  920. X#endif
  921. X           if (flags & 8) if ((flags & 192) != 192) { VCF; return -1; }
  922. X
  923. X#ifdef NO_FDPASSING
  924. X               if ((fdtty = open(fntty,O_RDWR)) == -1)
  925. X         return -1;
  926. X           if ((fdin = dup(fdre)) == -1)
  927. X        {
  928. X         (void) close(fdtty);
  929. X         fdtty = -1;
  930. X         return -1;
  931. X        }
  932. X           if ((fdout = dup(fdre)) == -1)
  933. X        {
  934. X         (void) close(fdtty);
  935. X         fdtty = -1;
  936. X         (void) close(fdout);
  937. X         fdout = -1;
  938. X         return -1;
  939. X        }
  940. X#endif
  941. X           VCF; /* yahoo! */
  942. X           (void) close(open(fntty,O_RDWR));
  943. X           /* XXX: do we really have to reattach? */
  944. X           /* I wish there were no concept of controlling tty. */
  945. X           /* Instead, an ioctl on /dev/tty (i.e., fd 3) would */
  946. X           /* return a session identifier. */
  947. X
  948. X           if (fdpass != -1)
  949. X        {
  950. X         if (pty_sendint(fdpass,'G',&siglerpid) == -1)
  951. X           return -1;
  952. X           /* XXX: death(1) might be more intuitive. Then */
  953. X           /* again, it may also be much more destructive. */
  954. X         if (pty_sendfd(fdpass,'m',&fdmty) == -1)
  955. X           return -1;
  956. X         if (pty_sendfd(fdpass,'s',&fdsty) == -1)
  957. X           return -1;
  958. X        }
  959. X
  960. X           /* So that we can disconnect again, we have to reset the */
  961. X           /* siglerpid in fdsess. That done, we've totally severed */
  962. X           /* our previous link to a connection. */
  963. X               (void) lseek(fdsess,(long) sizeof(int),0);
  964. X               (void) write(fdsess,(char *) &siglerpid,sizeof(int));
  965. X
  966. X           flagsigler = 1;
  967. X           (void) setpgrp(0,pgrp);
  968. X           (void) kill(pid,SIGUSR1); /* grrrr */
  969. X           return 0;
  970. X     default: (void) pty_putch(fdre," "); break;
  971. X    }
  972. X VCF;
  973. X return -1;
  974. X}
  975. X
  976. Xstruct timeval instant = { 0, 0 };
  977. X
  978. Xvoid master(fnsty,child)
  979. Xchar *fnsty;
  980. Xint child;
  981. X{
  982. X fd_set rfds;
  983. X fd_set wfds;
  984. X int fdnum;
  985. X int r;
  986. X
  987. X /* XXX: is it a race for child to set pty modes? */
  988. X
  989. X /* Note that we don't close fdsty. */
  990. X
  991. X siglerpid = getppid();
  992. X slavepid = child;
  993. X pid = getpid();
  994. X glfnsty = fnsty;
  995. X
  996. X if (flagsession)
  997. X  {
  998. X   /* Security note: This is the only file we actually create, */
  999. X   /* not counting the reconnect socket. */
  1000. X   (void) sprintf(fnsess,"sess.%s",fnsty + sizeof(DEVSTY) - 3);
  1001. X   fdsess = open(fnsess,O_RDWR | O_CREAT | O_TRUNC,0600);
  1002. X   (void) write(fdsess,(char *) &uid,sizeof(int));
  1003. X   (void) write(fdsess,(char *) &siglerpid,sizeof(int));
  1004. X   (void) write(fdsess,(char *) &pid,sizeof(int));
  1005. X   (void) write(fdsess,(char *) &slavepid,sizeof(int));
  1006. X   /* We'll never actually bother closing fdsess. Who cares? */
  1007. X  }
  1008. X
  1009. X sig_ignore(SIGURG);
  1010. X sig_ignore(SIGIO);
  1011. X sig_ignore(SIGHUP);
  1012. X sig_ignore(SIGQUIT);
  1013. X sig_ignore(SIGINT);
  1014. X sig_sethandler(SIGXCPU,sig_force); sig_handle(SIGXCPU);
  1015. X sig_ignore(SIGXFSZ);
  1016. X sig_ignore(SIGPROF);
  1017. X sig_ignore(SIGVTALRM);
  1018. X
  1019. X sig_default(SIGEMT); /* XXX: really dump? */
  1020. X sig_default(SIGIOT);
  1021. X sig_default(SIGTRAP);
  1022. X sig_default(SIGSYS);
  1023. X sig_default(SIGFPE);
  1024. X sig_default(SIGILL);
  1025. X sig_default(SIGSEGV);
  1026. X
  1027. X sig_default(SIGSTOP);
  1028. X
  1029. X sig_sethandler(SIGTTIN,sig_ttin); sig_handle(SIGTTIN);
  1030. X sig_sethandler(SIGTTOU,sig_ttou); sig_handle(SIGTTOU);
  1031. X sig_sethandler(SIGTSTP,sig_tstp); sig_handle(SIGTSTP);
  1032. X sig_sethandler(SIGUSR1,sig_cont); sig_handle(SIGUSR1);
  1033. X sig_ignore(SIGCONT); /* grrrr. see explanation above sig_cont. */
  1034. X sig_sethandler(SIGPIPE,sig_pipe); sig_handle(SIGPIPE);
  1035. X
  1036. X sig_sethandler(SIGCHLD,sig_chld); sig_handle(SIGCHLD);
  1037. X
  1038. X sig_sethandler(SIGTERM,sig_term); sig_handle(SIGTERM);
  1039. X sig_sethandler(SIGWINCH,sig_winch); sig_handle(SIGWINCH);
  1040. X
  1041. X sig_sethandler(SIGUSR2,sig_usr2); sig_handle(SIGUSR2);
  1042. X
  1043. X if (fdpass != -1)
  1044. X  {
  1045. X   if (pty_sendint(fdpass,'G',&siglerpid) == -1)
  1046. X     death(1);
  1047. X   if (pty_sendfd(fdpass,'m',&fdmty) == -1)
  1048. X     death(1);
  1049. X   if (pty_sendfd(fdpass,'s',&fdsty) == -1)
  1050. X     death(1);
  1051. X  }
  1052. X
  1053. X#define SET_FDNUM fdnum = fdin; if (fdout > fdnum) fdnum = fdout; \
  1054. Xif (fdmty > fdnum) fdnum = fdmty; fdnum++;
  1055. X
  1056. X SET_FDNUM
  1057. X
  1058. X if (fdpass == -1)
  1059. X   (void) fcntl(fdmty,F_SETFL,FNDELAY);
  1060. X   /* If it doesn't work, too bad. */
  1061. X
  1062. X#ifdef SIGINTERRUPT
  1063. X sig_interrupt();
  1064. X#endif
  1065. X
  1066. X for (;;)
  1067. X  {
  1068. X   /* Stage 1: Mangle internal states. This could be made into a */
  1069. X   /* critical section, but there's no point. */
  1070. X
  1071. X   if ((flagconnected == 2) && (flagchild != 2))
  1072. X     flagconnected = 1 + 2 * (flagchild == 0);
  1073. X   if ((flagconnected != 0) && (flagsigler == 0))
  1074. X    {
  1075. X     flagconnected = 0;
  1076. X     if (flagsession)
  1077. X      {
  1078. X       (void) kill(siglerpid,SIGTERM);
  1079. X#ifdef NO_SESSION
  1080. X       ; /* impossible */
  1081. X#else
  1082. X       if (disconnect(fnsty) == -1)
  1083. X     quickdeath(1); /* XXX: sigh */
  1084. X       if (fdnum <= fdre)
  1085. X     fdnum = fdre + 1;
  1086. X#endif
  1087. X      }
  1088. X    }
  1089. X
  1090. X   /* Stage 2: Prepare fds, and select(). */
  1091. X
  1092. X   FD_ZERO(&rfds);
  1093. X   FD_ZERO(&wfds);
  1094. X
  1095. X   if ((fdpass == -1) && (outbuflen < outbufsiz))
  1096. X     FD_SET(fdmty,&rfds);
  1097. X   if ((fdpass == -1) && ptybuflen)
  1098. X     FD_SET(fdmty,&wfds);
  1099. X   if ((fdpass == -1)
  1100. X     &&(ptybuflen < ptybufsiz) && (flagsigler == 1)
  1101. X     &&(flagconnected == 1) && (flagchild == 1))
  1102. X     FD_SET(fdin,&rfds);
  1103. X   if ((fdpass == -1)
  1104. X     &&(outbuflen) && (flagsigler == 1) && (flagconnected == 1))
  1105. X     FD_SET(fdout,&wfds);
  1106. X
  1107. X   if (flagsession && (flagconnected == 0))
  1108. X     FD_SET(fdre,&rfds);
  1109. X
  1110. X   /* The times to flush buffers: when the child has stopped and we're */
  1111. X   /* connected; when the child has died and we're connected; when the */
  1112. X   /* signaller has died and we don't support sessions. */
  1113. X   if (((flagconnected == 1) && (flagchild != 1))
  1114. X     ||((flagconnected == 0) && (flagsession == 0)))
  1115. X     r = select(fdnum,&rfds,&wfds,(fd_set *) 0,&instant);
  1116. X   else
  1117. X     r = select(fdnum,&rfds,&wfds,(fd_set *) 0,(struct timeval *) 0);
  1118. X
  1119. X
  1120. X   /* Stage 3: Interpret the results and handle special cases. */
  1121. X
  1122. X   if (r <= 0)
  1123. X     if (r == -1)
  1124. X       switch(errno)
  1125. X        {
  1126. X         case EBADF: death(1);
  1127. X                     break;
  1128. X         case EINTR: break; /* fine. */
  1129. X         case EINVAL: break; /* impossible. */
  1130. X         default: break; /* say what? */
  1131. X        }
  1132. X     else /* r is 0 */
  1133. X      {
  1134. X       if (flagconnected == 1) /* flagchild is 0 or 2 */
  1135. X     if (flagchild == 0)
  1136. X       break; /* That's it! Child died, and we're outta here! */
  1137. X     else
  1138. X      { /* done with flush, time to stop sigler & idle */
  1139. X       if (flagjobctrl)
  1140. X        {
  1141. X         /* As usual, if we don't have a tty, tmotty == tmochartty
  1142. X        and it won't matter that fdtty is undefined. */
  1143. X         (void) setpgrp(0,pgrp);
  1144. X           if (tty_modifymodes(fdtty,&tmotty,&tmochartty) == -1)
  1145. X           ; /* XXX: what to do? */
  1146. X         (void) setpgrp(0,pid);
  1147. X         switch(childsig)
  1148. X          {
  1149. X           case SIGSTOP: (void) kill(siglerpid,SIGSTOP); break;
  1150. X           case SIGTTOU: (void) kill(siglerpid,SIGTTOU); break;
  1151. X           case SIGTTIN: (void) kill(siglerpid,SIGTTIN); break;
  1152. X           case SIGTSTP: (void) kill(siglerpid,SIGTSTP); break;
  1153. X           case SIGCONT: break; /* special case---see sig_tstp */
  1154. X           default: (void) kill(siglerpid,SIGSTOP); break;
  1155. X          }
  1156. X         flagconnected = 2;
  1157. X        }
  1158. X      }
  1159. X       else if (flagconnected == 0) /* non-session, sigler dead */
  1160. X     break; /* Giving pty pgrp a HUP, ho hum */
  1161. X     /* Most pgrp-based killing would be more logically done */
  1162. X     /* one process at a time, i.e., we should give our child */
  1163. X     /* a signal specially. But nobody else does, so we won't. */
  1164. X      }
  1165. X   else
  1166. X    {
  1167. X#ifndef NO_SESSION
  1168. X     if (flagconnected == 0)
  1169. X       if (FD_ISSET(fdre,&rfds))
  1170. X     if (reconnect() == -1)
  1171. X      {
  1172. X       if (disconnect(fnsty) == -1)
  1173. X         quickdeath(1); /* sigh */
  1174. X           if (fdnum <= fdre)
  1175. X         fdnum = fdre + 1;
  1176. X          }
  1177. X     else
  1178. X      {
  1179. X       flagconnected = 1; /* yay! */
  1180. X       SET_FDNUM
  1181. X       continue; /* XXX */
  1182. X      }
  1183. X#endif
  1184. X
  1185. X
  1186. X   /* Stage 4: Do normal I/O. */
  1187. X
  1188. X#ifdef SIGINTERRUPT
  1189. X     sig_startring(); /* blocking? never heard of it */
  1190. X#endif
  1191. X
  1192. X     if (FD_ISSET(fdin,&rfds))
  1193. X      {
  1194. X       /* ptybuflen must be smaller than ptybufsiz. */
  1195. X       r = read(fdin,ptybuf + ptybuflen,ptybufsiz - ptybuflen);
  1196. X       if (r == -1)
  1197. X     switch(errno)
  1198. X      {
  1199. X       case EINTR: case EWOULDBLOCK: break; /* fine */
  1200. X       default: death(1);
  1201. X      }
  1202. X       else if (r == 0) /* EOF */
  1203. X    {
  1204. X     ; /* XXX: there's no way to pass an EOF */
  1205. X    }
  1206. X       else
  1207. X     ptybuflen += r;
  1208. X      }
  1209. X     if (FD_ISSET(fdmty,&rfds))
  1210. X      {
  1211. X       /* outbuflen must be smaller than outbufsiz. */
  1212. X       r = read(fdmty,outbuf + outbuflen,outbufsiz - outbuflen);
  1213. X       if (r == -1)
  1214. X     switch(errno)
  1215. X      {
  1216. X       case EINTR: case EWOULDBLOCK: break; /* fine */
  1217. X       default: death(1);
  1218. X      }
  1219. X       else if (r == 0) /* EOF */
  1220. X    {
  1221. X     ; /* This can't happen. The slave can't pass an EOF. */
  1222. X     /* XXX: Should we close fdout anyway? */
  1223. X    }
  1224. X       else
  1225. X     outbuflen += r;
  1226. X      }
  1227. X     if (FD_ISSET(fdout,&wfds))
  1228. X      {
  1229. X       r = write(fdout,outbuf,outbuflen);
  1230. X       if (r == -1)
  1231. X     switch(errno)
  1232. X      {
  1233. X       case EINTR: case EWOULDBLOCK: break; /* fine */
  1234. X       default: death(1);
  1235. X      }
  1236. X       else if (r == 0) /* ? */
  1237. X     ; /* impossible */
  1238. X       else if (r == outbuflen)
  1239. X     outbuflen = 0;
  1240. X       else
  1241. X    {
  1242. X     outbuflen -= r;
  1243. X         copy(outbuf,outbuf + r,outbuflen);
  1244. X    }
  1245. X      }
  1246. X     if (FD_ISSET(fdmty,&wfds))
  1247. X      {
  1248. X       r = write(fdmty,ptybuf,ptybuflen);
  1249. X       if (r == -1)
  1250. X     switch(errno)
  1251. X      {
  1252. X       case EINTR: case EWOULDBLOCK: break; /* fine */
  1253. X       default: death(1);
  1254. X      }
  1255. X       else if (r == 0) /* ? */
  1256. X     ; /* impossible */
  1257. X       else if (r == ptybuflen)
  1258. X     ptybuflen = 0;
  1259. X       else
  1260. X    {
  1261. X     ptybuflen -= r;
  1262. X         copy(ptybuf,ptybuf + r,ptybuflen);
  1263. X    }
  1264. X      }
  1265. X
  1266. X#ifdef SIGINTERRUPT
  1267. X     sig_stopring();
  1268. X#endif
  1269. X    }
  1270. X  }
  1271. X
  1272. X death(0);
  1273. X}
  1274. END_OF_FILE
  1275.   if test 18086 -ne `wc -c <'master.c'`; then
  1276.     echo shar: \"'master.c'\" unpacked with wrong size!
  1277.   fi
  1278.   # end of 'master.c'
  1279. fi
  1280. if test -f 'pty.c' -a "${1}" != "-c" ; then 
  1281.   echo shar: Will not clobber existing file \"'pty.c'\"
  1282. else
  1283.   echo shar: Extracting \"'pty.c'\" \(10563 characters\)
  1284.   sed "s/^X//" >'pty.c' <<'END_OF_FILE'
  1285. X/* Copyright 1990, Daniel J. Bernstein. All rights reserved. */
  1286. X
  1287. X/*
  1288. Xpty.c: run a program under a pty session
  1289. X*/
  1290. X
  1291. X#include <stdio.h>
  1292. Xextern unsigned short getuid(); /* grrrr */
  1293. Xextern unsigned short geteuid(); /* grrrr */
  1294. X#include "config.h"
  1295. X#include "getopt.h"
  1296. X#include "err.h"
  1297. X#include "pty.h"
  1298. X#include "tty.h"
  1299. X#include "texts.h"
  1300. X#include "sig.h"
  1301. X#include "sigler.h"
  1302. X#include "master.h"
  1303. X#include "slave.h"
  1304. X#include "file.h"
  1305. X#include "logs.h"
  1306. X#include "misc.h"
  1307. X
  1308. Xint flagpcbreak = 0; /* -pc, character-at-a-time */
  1309. Xint flagpnew = 1; /* -pd, new line discipline---traditionally off to start */
  1310. Xint flagpecho = 1; /* -pe, echo characters */
  1311. Xint flagpcrmod = 1; /* -pn, munge carriage returns */
  1312. Xint flagpraw = 0; /* -pr, raw mode */
  1313. Xint flagpcrt = 1; /* -ps, screen */
  1314. X
  1315. Xgetfreepty(fnmty,fnsty,pty1,pty2)
  1316. Xregister char fnmty[sizeof(DEVMTY)];
  1317. Xregister char fnsty[sizeof(DEVSTY)];
  1318. Xregister char pty1[sizeof(PTY1)];
  1319. Xregister char pty2[sizeof(PTY2)];
  1320. X{
  1321. X register char *c1;
  1322. X register char *c2;
  1323. X register char *c1start; /* for ``random'' pty searching */
  1324. X register char *c2start;
  1325. X int e;
  1326. X
  1327. X if (flagxrandom)
  1328. X  {
  1329. X   c1start = pty1 + (pid % (sizeof(PTY1) - 1));
  1330. X   c2start = pty2 + ((pid + date) % (sizeof(PTY2) - 1));
  1331. X  }
  1332. X else
  1333. X  {
  1334. X   c1start = pty1;
  1335. X   c2start = pty2;
  1336. X  }
  1337. X
  1338. X c1 = c1start;
  1339. X do
  1340. X  {
  1341. X   fnmty[sizeof(DEVMTY) - 3] = *c1;
  1342. X   fnmty[sizeof(DEVMTY) - 2] = pty2[0];
  1343. X   if (!access(fnmty,F_OK))
  1344. X    {
  1345. X     c2 = c2start;
  1346. X     fnsty[sizeof(DEVSTY) - 3] = *c1;
  1347. X     fnmty[sizeof(DEVMTY) - 2] = fnsty[sizeof(DEVSTY) - 2] = *c2;
  1348. X     do
  1349. X      {
  1350. X#ifdef DESPERATE_ALARMS
  1351. X       sig_startring();
  1352. X#endif
  1353. X
  1354. X/* Some other process could come along and mess up our test by opening */
  1355. X/* the master side before we do. But in that case they'll get the pty */
  1356. X/* anyway, and we'll move on to another possibility without comment. */
  1357. X       if (flagxchkopen)
  1358. X    {
  1359. X#ifdef DONT_NDELAY
  1360. X         fdsty = open(fnsty,O_RDWR);
  1361. X#else
  1362. X         fdsty = open(fnsty,O_RDWR | O_NDELAY);
  1363. X#endif
  1364. X         e = errno;
  1365. X         fdmty = open(fnmty,O_RDWR);
  1366. X    }
  1367. X       else
  1368. X    {
  1369. X         fdmty = open(fnmty,O_RDWR);
  1370. X         fdsty = open(fnsty,O_RDWR);
  1371. X     e = errno;
  1372. X    }
  1373. X
  1374. X#ifdef DESPERATE_ALARMS
  1375. X       sig_stopring();
  1376. X#endif
  1377. X
  1378. X       if (fdmty != -1)
  1379. X    {
  1380. X     if (flagxskipopen && (fdsty != -1))
  1381. X       warnerr2("pty: warning: slave %s still in use\n",fnsty);
  1382. X     else
  1383. X      {
  1384. X       if ((fdsty == -1) && (e != EINTR) && (e != EWOULDBLOCK))
  1385. X         fatalerr2p(6,"pty: fatal: slave %s unopenable",fnsty,e);
  1386. X       if (flagxchkopen)
  1387. X         if (fdsty == -1)
  1388. X          {
  1389. X           fdsty = open(fnsty,O_RDWR);
  1390. X           e = errno;
  1391. X          }
  1392. X         else
  1393. X           warnerr2("pty: warning: slave %s still in use\n",fnsty);
  1394. X       if (fdsty == -1)
  1395. X         fatalerr2p(6,"pty: fatal: slave %s unopenable",fnsty,e);
  1396. X       else
  1397. X        {
  1398. X         if (flagxchkopen)
  1399. X           if (fcntl(fdsty,F_SETFL,0) == -1)
  1400. X         fatalerrp(6,"pty: fatal: can't fcntl pty",e);
  1401. X         return 0;
  1402. X        }
  1403. X      }
  1404. X    }
  1405. X
  1406. X       if (fdmty != -1) (void) close(fdmty);
  1407. X       if (fdsty != -1) (void) close(fdsty);
  1408. X       if (!(*(++c2)))
  1409. X     c2 = pty2;
  1410. X       fnmty[sizeof(DEVMTY) - 2] = fnsty[sizeof(DEVSTY) - 2] = *c2;
  1411. X      }
  1412. X     while (c2 != c2start);
  1413. X    }
  1414. X   if (!(*(++c1)))
  1415. X     c1 = pty1;
  1416. X  }
  1417. X while (c1 != c1start);
  1418. X return -1;
  1419. X}
  1420. X
  1421. Xchar fnmty[sizeof(DEVMTY)] = DEVMTY;
  1422. Xchar fnsty[sizeof(DEVSTY)] = DEVSTY;
  1423. Xchar pty1[sizeof(PTY1)] = PTY1;
  1424. Xchar pty2[sizeof(PTY2)] = PTY2;
  1425. X
  1426. Xmain(argc,argv)
  1427. Xint argc;
  1428. Xchar *argv[];
  1429. X{
  1430. X int opt;
  1431. X int f;
  1432. X
  1433. X uid = getuid();
  1434. X euid = geteuid();
  1435. X pid = getpid();
  1436. X pgrp = getpgrp(0);
  1437. X date = now();
  1438. X setusername();
  1439. X
  1440. X while ((opt = getopt(argc,argv,"qQvdDe3Ef:FjJsStTp:x:0ACHUVW")) != EOF)
  1441. X   switch(opt)
  1442. X    {
  1443. X     case 'A': fatalinfo(1,ptyauthor);
  1444. X     case 'C': fatalinfo(1,ptycopyright);
  1445. X     case 'H': fatalinfo(1,ptyhelp);
  1446. X     case 'U': fatalinfo(1,ptyusage);
  1447. X     case 'V': fatalinfo(1,ptyversion);
  1448. X     case 'W': fatalinfo(1,ptywarranty);
  1449. X     case '?': fatalinfo(1,ptyusage);
  1450. X     case 'q': flagquiet = 1; break;
  1451. X     case 'Q': flagquiet = 0; flagverbose = 0; break;
  1452. X     case 'v': flagverbose = 1; break;
  1453. X     case 'd': flagdetached = 1; flagjobctrl = 0; flagttymodes = 0; break;
  1454. X     case 'D': flagdetached = 0; flagjobctrl = 1; flagttymodes = 1; break;
  1455. X     case 'e': flagsameerr = 2; break;
  1456. X     case '3': flagsameerr = 1; break;
  1457. X     case 'E': flagsameerr = 0; break;
  1458. X     case 'f': flagfdpass = 1; 
  1459. X           if (sscanf(optarg,"%d",&fdpass) < 1) fatalinfo(1,ptyusage);
  1460. X           break;
  1461. X     case 'F': flagfdpass = 0; break;
  1462. X     case 'j': flagjobctrl = 1; break;
  1463. X     case 'J': flagjobctrl = 0; break;
  1464. X     case 's': flagsession = 1; flagxutmp = 1; break;
  1465. X     case 'S': flagsession = 0; flagxutmp = 0; break;
  1466. X     case 't': flagttymodes = 1; break;
  1467. X     case 'T': flagttymodes = 0; break;
  1468. X     case '0': flagsameerr = 2; flagsession = 0; flagttymodes = 0;
  1469. X           flagxutmp = 0; /* XXX: also flagxwtmp = 0? */
  1470. X           flagpcbreak = 3; flagpraw = 3; flagpecho = 2; flagpnew = 2;
  1471. X           break;
  1472. X     case 'p': while (opt = *(optarg++))
  1473. X         switch(opt)
  1474. X          {
  1475. X           case 'c': flagpcbreak = 3; break;
  1476. X           case 'C': flagpcbreak = 2; break;
  1477. X           case 'd': flagpnew = 3; break;
  1478. X           case 'D': flagpnew = 2; break;
  1479. X           case 'e': flagpecho = 3; break;
  1480. X           case 'E': flagpecho = 2; break;
  1481. X           case 'n': flagpcrmod = 3; break;
  1482. X           case 'N': flagpcrmod = 2; break;
  1483. X           case 'r': flagpraw = 3; break;
  1484. X           case 'R': flagpraw = 2; break;
  1485. X           case 's': flagpcrt = 3; break;
  1486. X           case 'S': flagpcrt = 2; break;
  1487. X           case '0': flagpcbreak = 3; flagpraw = 3;
  1488. X                 flagpecho = 2; flagpnew = 2;
  1489. X                 break;
  1490. X           default: fatalinfo(1,ptyusage); break;
  1491. X          }
  1492. X               break;
  1493. X     case 'x': while (opt = *(optarg++))
  1494. X         switch(opt)
  1495. X          {
  1496. X           case 'c': flagxchown = 1; break;
  1497. X           case 'C': flagxchown = 0; break;
  1498. X           case 'u': flagxutmp = 1; break;
  1499. X           case 'U': flagxutmp = 0; break;
  1500. X           case 'w': flagxwtmp = 1; break;
  1501. X           case 'W': flagxwtmp = 0; break;
  1502. X           case 'x': flagxexcl = 1; break;
  1503. X           case 'X': flagxexcl = 0; break;
  1504. X           case 'e': flagxerrwo = 1; break;
  1505. X           case 'E': flagxerrwo = 0; break;
  1506. X           case 'n': flagxchkopen = 1; break;
  1507. X           case 'N': flagxchkopen = 0; break;
  1508. X           case 'o': flagxskipopen = 1; break;
  1509. X           case 'O': flagxskipopen = 0; break;
  1510. X           case 'r': flagxrandom = 1; break;
  1511. X           case 'R': flagxrandom = 0; break;
  1512. X           case 's': flagxsetuid = 1; break;
  1513. X           case 'S': flagxsetuid = 0; break;
  1514. X           default: fatalinfo(1,ptyusage); break;
  1515. X          }
  1516. X               break;
  1517. X    }
  1518. X argv += optind, argc -= optind;
  1519. X
  1520. X if (!*argv)
  1521. X   fatalinfo(1,ptyusage);
  1522. X
  1523. X /* Option forcing. */
  1524. X#ifdef NO_UTMP
  1525. X  if (flagxutmp) if (flagverbose) warnerr2("%s","pty: utmp forced off\n");
  1526. X  flagxutmp = 0;
  1527. X#endif
  1528. X#ifdef NO_WTMP
  1529. X  if (flagxwtmp) if (flagverbose) warnerr2("%s","pty: wtmp forced off\n");
  1530. X  flagxwtmp = 0;
  1531. X#endif
  1532. X#ifdef NO_CHOWN
  1533. X  if (flagxchown) if (flagverbose) warnerr2("%s","pty: chown forced off\n");
  1534. X  flagxchown = 0;
  1535. X#endif
  1536. X#ifdef NO_SESSION
  1537. X  if (flagsession) if (flagverbose) warnerr2("%s","pty: session forced off\n");
  1538. X  flagsession = 0;
  1539. X#endif
  1540. X#ifdef MUST_UTMP
  1541. X  if (flagxutmp) if (flagverbose) warnerr2("%s","pty: utmp forced on\n");
  1542. X  flagxutmp = 1;
  1543. X#endif
  1544. X#ifdef MUST_WTMP
  1545. X  if (flagxwtmp) if (flagverbose) warnerr2("%s","pty: wtmp forced on\n");
  1546. X  flagxwtmp = 1;
  1547. X#endif
  1548. X#ifdef MUST_CHOWN
  1549. X  if (flagxchown) if (flagverbose) warnerr2("%s","pty: chown forced on\n");
  1550. X  flagxchown = 1;
  1551. X#endif
  1552. X#ifdef MUST_SESSION
  1553. X  if (flagsession) if (flagverbose) warnerr2("%s","pty: session forced on\n");
  1554. X  flagsession = 1;
  1555. X#endif
  1556. X#ifdef NO_FDPASSING
  1557. X  if (flagfdpass) if (flagverbose) warnerr2("%s","pty: fd passing forced off\n");
  1558. X  flagfdpass = 0;
  1559. X#endif
  1560. X
  1561. X /* Option munging. */
  1562. X if (flagsession) flagsameerr = 0;
  1563. X if (flagdetached) flagttymodes = 0;
  1564. X if (flagxskipopen) flagxchkopen = 1;
  1565. X
  1566. X if (!flagxsetuid)
  1567. X  {
  1568. X   (void) setreuid(uid,uid);
  1569. X   euid = uid;
  1570. X  }
  1571. X
  1572. X
  1573. X sig_init();
  1574. X sig_sethandler(SIGALRM,nothing); sig_handle(SIGALRM);
  1575. X sig_default(SIGTTIN);
  1576. X sig_default(SIGTTOU);
  1577. X
  1578. X if (fdpass == -1) /* wow, was this a source of bugs. */
  1579. X  {
  1580. X   fdin = 0;
  1581. X   fdout = 1;
  1582. X  }
  1583. X
  1584. X if (flagdetached)
  1585. X  {
  1586. X   tty_initmodes(&tmopty,flagpcbreak,flagpnew,flagpecho,
  1587. X             flagpcrmod,flagpraw,flagpcrt);
  1588. X  }
  1589. X else
  1590. X  {
  1591. X   if ((fdtty = tty_getctrl()) == -1)
  1592. X     fatalerr(2,"pty: fatal: cannot find control terminal; try -d?\n");
  1593. X   if (tty_getmodes(fdtty,&tmotty) == -1)
  1594. X     fatalerr(3,"pty: fatal: cannot get current tty modes\n");
  1595. X     /* XXX: is there a way to recover more gracefully? */
  1596. X   tty_copymodes(&tmopty,&tmotty);
  1597. X   tty_mungemodes(&tmopty,flagpcbreak,flagpnew,flagpecho,
  1598. X              flagpcrmod,flagpraw,flagpcrt);
  1599. X   tty_copymodes(&tmochartty,&tmotty);
  1600. X   if (flagttymodes)
  1601. X     tty_charmode(&tmochartty);
  1602. X  }
  1603. X
  1604. X /* XXX: Here would be a good spot to include pty limits, say through */
  1605. X /* the file PTYDIR/LIMITS. Lines of the form user group num, saying */
  1606. X /* that user in that group is limited to num ptys, with * for all. */
  1607. X /* All pty use would have to be logged somewhere. Anyway, with a */
  1608. X /* streams-based pty, there wouldn't be much point to limits. */
  1609. X
  1610. X if (getfreepty(fnmty,fnsty,pty1,pty2) == -1)
  1611. X   fatalerr(5,"pty: fatal: no ptys available\n");
  1612. X
  1613. X if (flagverbose)
  1614. X   warnerr2("pty: successfully opened pty %s\n",fnsty);
  1615. X
  1616. X if (tty_modifymodes(fdtty,&tmochartty,&tmotty) == -1)
  1617. X  {
  1618. X   (void) tty_setmodes(fdtty,&tmotty); /* XXX --- gasp */
  1619. X   fatalerr(4,"pty: fatal: cannot set modes of original tty\n");
  1620. X  }
  1621. X
  1622. X/* In general, BSD systems check MAXUPRC against the effective uid, */
  1623. X/* rather than the real uid; and they check it during a fork(). */
  1624. X/* The combination of these annoying behaviors means that we have */
  1625. X/* to switch uids while forking, hence possibly losing any security */
  1626. X/* measures we may have set up before the fork(). Grrrr. */
  1627. X
  1628. X (void) setreuid(euid,uid);
  1629. X if ((f = fork()) == -1)
  1630. X  {
  1631. X   (void) tty_modifymodes(fdtty,&tmotty,&tmochartty);
  1632. X   fatalerr(7,"pty: fatal: cannot fork once\n");
  1633. X   /* After this, the signaller will handle tty modes. */
  1634. X  }
  1635. X else if (f == 0)
  1636. X   if ((f = fork()) == -1)
  1637. X    {
  1638. X     (void) kill(pid,SIGTERM); /*XXX*/
  1639. X     fatalerr(7,"pty: fatal: cannot fork twice\n");
  1640. X    }
  1641. X   else if (f == 0)
  1642. X    {
  1643. X     (void) setreuid(uid,euid);
  1644. X     slave(fnsty,argv);
  1645. X    }
  1646. X   else
  1647. X    {
  1648. X     (void) setreuid(uid,euid);
  1649. X     if (flagsession)
  1650. X       if (sessdir() == -1)
  1651. X         fatal(1);
  1652. X     master(fnsty,f);
  1653. X    }
  1654. X else
  1655. X  {
  1656. X   (void) setreuid(uid,euid);
  1657. X   if (flagsession)
  1658. X     if (sessdir() == -1)
  1659. X      {
  1660. X       fatalerr(8,"pty: fatal: cannot change to session directory\n");
  1661. X       (void) tty_modifymodes(fdtty,&tmotty,&tmochartty);
  1662. X      }
  1663. X   sigler(fnsty,f);
  1664. X  }
  1665. X
  1666. X fatal(9); /* just in case */
  1667. X /*NOTREACHED*/
  1668. X}
  1669. END_OF_FILE
  1670.   if test 10563 -ne `wc -c <'pty.c'`; then
  1671.     echo shar: \"'pty.c'\" unpacked with wrong size!
  1672.   fi
  1673.   # end of 'pty.c'
  1674. fi
  1675. echo shar: End of archive 2 \(of 6\).
  1676. cp /dev/null ark2isdone
  1677. MISSING=""
  1678. for I in 1 2 3 4 5 6 ; do
  1679.     if test ! -f ark${I}isdone ; then
  1680.     MISSING="${MISSING} ${I}"
  1681.     fi
  1682. done
  1683. if test "${MISSING}" = "" ; then
  1684.     echo You have unpacked all 6 archives.
  1685.     rm -f ark[1-9]isdone
  1686. else
  1687.     echo You still must unpack the following archives:
  1688.     echo "        " ${MISSING}
  1689. fi
  1690. exit 0
  1691. exit 0 # Just in case...
  1692.